﻿
package extremefx.tools {

	/**
	 * @author Marcelo Volmaro
	 */
	public final class ArrayUtils {
		public static function remove(pArray:Array, pObject:*):int {
			if (!pArray || !pArray.length) return -1;
			
			var idx:int = pArray.indexOf(pObject);
			if (idx != -1) pArray.splice(idx, 1);
			return idx;
		}
		
		public static function insertUnique(pArray:Array, pObject:*):void {
			var idx:int = pArray.indexOf(pObject);
			if (idx == -1) {
				idx = pArray.length;
			}
			
			pArray[idx] = pObject;
		}
		
		public static function removeDuplicates(inArray:Array):Array {
			return inArray.filter(function (e:*, i:int, inArray:Array):Boolean {
				return (i == 0) ? true : inArray.lastIndexOf(e, i - 1) == -1;
			});
		}
		
		public static function equals(pFirst:Array, pSecond:Array):Boolean {
			var i:uint = pFirst.length;
			if (i != pSecond.length)
				return false;
			
			while (i--)
				if (pFirst[i] != pSecond[i])
					return false;
			
			return true;
		}
	}
}
